package com.project.service;

import com.project.common.utils.Base64Util;
import com.project.common.utils.FileUtils;
import com.project.common.utils.Util;
import com.project.dao.OrgInfoMapper;
import com.project.entity.Image;
import com.project.entity.OrgInfo;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CryFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 14:38
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class OrgInfoServiceImpl extends BaseService implements OrgInfoService {
    @Autowired
    private OrgInfoMapper orgInfoMapper;

    @Autowired
    private ImagesService imagesService;

    @Autowired
    private CryFileUtil cryFileUtil;


    @Override
    public int addOrgInfo(OrgInfo orgInfo) throws Exception {

        //首先插入用人单位基础数据（除图片外）
        orgInfoMapper.insertSelective(orgInfo);

        OrgInfo orgInfo1 = new OrgInfo();
        orgInfo1.setId(orgInfo.getId());
        System.out.println("addOrgInfo: " + orgInfo1.getId());
        //文件操作
        //1. 获取root上级目录
        String rootPath = Util.getWebRootURL(this);

        //公司外景
        if (!StringUtils.isEmpty(orgInfo.getOutsidepicBASE64())) {
            //2. 获取下级目录
            String outSubPath = configParameters.getOutSubPath();
            //3. 获取文件格式
            String outExt = Util.getBase64StringExt(orgInfo.getOutsidepicBASE64());
            //4. 设置文件名称
            String fileNamePrefix = "out_";
            String fileName = fileNamePrefix + orgInfo.getId() + "." + outExt;
            //5. 创建文件
            String outBase64Str = Util.getBase64StringData(orgInfo.getOutsidepicBASE64());
            FileUtils.writeDecodeBase64StrToFile(rootPath + outSubPath + fileName, outBase64Str);

            //插入图片
            Image image = new Image();
            image.setPath(fileName);
            image.setCategory(2); //单位外景
            imagesService.add(image);

            //将图片id插入到图片表中
            orgInfo1.setOutsidepic(image.getId());

        }
        //营业执照
        if (!StringUtils.isEmpty(orgInfo.getBusinessliscenseBASE64())) {
            //2. 获取下级目录
            String businessSubPath = configParameters.getBusinessSubPath();
            //3. 获取文件格式
            String extBusiness = Util.getBase64StringExt(orgInfo.getBusinessliscenseBASE64());
            //4. 设置文件名称
            String businessFileNamePrefix = "businessliscense_";
            String businessFileName = businessFileNamePrefix + orgInfo.getId() + "." + extBusiness;
            businessSubPath += businessFileName;
            //5. 创建文件
            String businessBase64Str = Util.getBase64StringData(orgInfo.getBusinessliscenseBASE64());
            //获得加密后的文件字节流
            byte[] data = cryFileUtil.encry(Base64Util.decryptBASE64(businessBase64Str));
            FileUtils.writeBytesToFile(rootPath + businessSubPath, data);

            Image image = new Image();
            image.setPath(businessFileName);
            image.setCategory(1); //营业执照
            image.setIsencrypted(1);//确认加密
            imagesService.add(image);

            orgInfo1.setBusinessliscense(image.getId());
        }
        //图片有插入才更新企业记录
        if (!StringUtils.isEmpty(orgInfo1.getBusinessliscense()) | !StringUtils.isEmpty(orgInfo1.getOutsidepic())) {
            modifyById(orgInfo1);

        }
        return 1;
    }

    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 通过id查找公司信息
     *
     * @param id
     * @return
     */
    @Override
    public OrgInfo findByid(Integer id) {
        return orgInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int modifyByManagerId(OrgInfo orgInfo) {
        return orgInfoMapper.updateByManageIdSelective(orgInfo);
    }

    /**
     * author: WheelChen
     * date: 2017-5-22
     * function: 根据managerid修改公司信息
     *
     * @param orgInfo
     * @return
     */
    @Override
    public int modifyByCreatorId(OrgInfo orgInfo) {
        return orgInfoMapper.updateBymodifyByCreatorIdSelective(orgInfo);
    }

    @Override
    public List<OrgInfo> findOrgInfoBatch() {
        return orgInfoMapper.selectAll();
    }

    @Override
    public List<OrgInfo> findByCondition(OrgInfo orgInfo) {
        return orgInfoMapper.selectByCondition(orgInfo);
    }

    @Override
    public OrgInfo findByManagerId(Integer managerid) {
        return orgInfoMapper.selectByManagerId(managerid);
    }

    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 根据id修改
     *
     * @param orgInfo
     * @return
     */
    @Override
    public int modifyById(OrgInfo orgInfo) throws Exception {
        //文件操作
        //1. 获取root上级目录
//        String rootPath = Util.getWebRootURL(this);

        //公司外景
        if (!StringUtils.isEmpty(orgInfo.getOutsidepicBASE64())) {
            //2. 获取下级目录
            String outSubPath = configParameters.getOutSubPath();
            //3. 获取文件格式
            String outExt = Util.getBase64StringExt(orgInfo.getOutsidepicBASE64());
            //4. 设置文件名称
            String fileNamePrefix = "out_";
            String fileName = fileNamePrefix + orgInfo.getId() + "." + outExt;
            //5. 创建文件
            String outBase64Str = Util.getBase64StringData(orgInfo.getOutsidepicBASE64());
//            FileUtils.writeDecodeBase64StrToFile(rootPath + outSubPath + fileName, outBase64Str);

            Image image = new Image();
            image.setPath(fileName);
            if (orgInfo.getOutsidepic() == null) { //没有插入图片的修改
                image.setCategory(2); //单位外景
                imagesService.add(image);
                orgInfo.setOutsidepic(image.getId());
            } else {
                image.setId(orgInfo.getOutsidepic());
                imagesService.update(image);
            }

        }

        //营业执照
        if (!StringUtils.isEmpty(orgInfo.getBusinessliscenseBASE64())) {
            //2. 获取下级目录
            String businessSubPath = configParameters.getBusinessSubPath();
            //3. 获取文件格式
            String extBusiness = Util.getBase64StringExt(orgInfo.getBusinessliscenseBASE64());
            //4. 设置文件名称
            String businessFileNamePrefix = "businessliscense_";
            String businessFileName = businessFileNamePrefix + orgInfo.getId() + "." + extBusiness;
            businessSubPath += businessFileName;
            //5. 创建文件
            String businessBase64Str = Util.getBase64StringData(orgInfo.getBusinessliscenseBASE64());
            //获得加密后的文件字节流
            byte[] data = cryFileUtil.encry(Base64Util.decryptBASE64(businessBase64Str));
//            FileUtils.writeBytesToFile(rootPath + businessSubPath, data);


            Image image = new Image();
            image.setPath(businessFileName);
            if (orgInfo.getBusinessliscense() == null) {//若图片一开始无
                image.setCategory(1); //营业执照
                image.setIsencrypted(1);//确认加密
                imagesService.add(image);
                orgInfo.setBusinessliscense(image.getId());
            } else {//若图片一开始有
                image.setId(orgInfo.getBusinessliscense());
                imagesService.update(image);
            }
        }


        int res = orgInfoMapper.updateByPrimaryKeySelective(orgInfo);

        return res;
    }


    /**
     * author: WheelChen
     * date: 2017/7/31
     * function: 查询所有公司信息
     *
     * @return
     */
    @Override
    public List<OrgInfo> findAll() {
        return orgInfoMapper.selectAll();
    }
}
